*******************************
* Table A8
*******************************
clear
use masterdata_county.dta

encode state, gen(state2)
gen senior = (pop_55above)/pop
gen whiteratio = white/pop
gen blackratio = black/pop
gen a1 = white/pop
gen a2 = black/pop
gen a3 = indian/pop
gen a4 = asian/pop
gen a5 = pacific/pop
gen a6 = otherace/pop
gen a7 = tworaces/pop
gen a8 = hispanic/pop
gen ethnic = 1 - (a1^2 + a2^2 + a3^2 + a4^2 + a5^2 + a6^2 + a7^2 +a8^2)
gen nohs = lessthanhs/pop_over25
gen hs = highschool/pop_over25 
gen loweredu = nohs + hs
gen college = bachelor/pop_over25
gen advanced = (master + professional + doctorate)/pop_over25
gen unemployment = civilian_unemployed/civilian
gen male_unemployment = male_umemployed/civilian_male
gen female_unemployment = female_umemployed/civilian_female
gen white_unemployment = whilte_unemployed/whilte_laborforce
gen publicsectorratio = public_sector/employed_civilian
gen manufacturingratio = manufacturing/employed_civilian
gen agricultureratio = agriculture/employed_civilian
gen foreignratio = foreign_born/pop
gen noinsuranceratio = noinsurance/insurance_pop

gen lpop = ln(pop +1)
gen lnpcincome = ln(pcincome+1)

macro define demographic "d_tradeusch_pw lpop senior whiteratio loweredu lnpcincome unemployment white_unemployment manufacturingratio foreignratio noinsuranceratio"

replace totaltaa = totaltaa + taa13
replace totaltaa_certified = totaltaa_certified + taa13_approved
replace totaffected = totaffected + taa13_estworker
replace totaffected_approved  = totaffected_approved  + taa13_estworker_app
replace totaffected_denied = totaffected_denied + taa13_estworker_denied

gen lntotaffected = ln(totaffected +1)
gen lntotaffected_app = ln(totaffected_approved +1)
gen lntotaffected_den = ln(totaffected_denied +1)

gen taaworkforce = totaffected/civilian
gen taaworkforce_app = totaffected_approved/civilian
gen taaworkforce_denied = totaffected_denied/civilian

gen certifyratio = totaltaa_certified/totaltaa
replace certifyratio = 0 if certifyratio ==.

gen repwinning = 0
replace repwinning = 1 if per_gop > per_dem

replace sanders_vote_prop  = sanders_vote_prop *100

eststo clear
eststo: quietly areg sanders_vote_prop totaltaa certifyratio  $demographic, a(state2) vce(cluster state2)
eststo: quietly areg sanders_vote_prop totaltaa lntotaffected_app $demographic, a(state2) vce(cluster state2)
eststo: quietly areg sanders_vote_prop totaltaa lntotaffected_app $demographic if repwinning ==0, a(state2) vce(cluster state2)
eststo: quietly areg sanders_vote_prop totaltaa lntotaffected_app $demographic if repwinning ==1, a(state2) vce(cluster state2)
esttab, star(* 0.1 ** 0.05 *** 0.01) ar2
